package com.system.fkjslee.hos;

import android.annotation.SuppressLint;
import android.content.Intent;
import android.os.Handler;
import android.os.Looper;
import android.os.Message;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.SimpleAdapter;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class NormalAppoint extends AppCompatActivity implements AdapterView.OnItemClickListener {

    private ListView lvExpert;
    private Handler handler;
    private String lvData;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_normal_appoint);

        initView();
    }

    @SuppressLint("HandlerLeak")
    private void initView() {
        Log.i("check", "NormalAppoint initView start");
        lvExpert = findViewById(R.id.lvExpert);

        lvExpert.setOnItemClickListener(this);
        handler = new Handler() {
            @Override
            public void handleMessage(Message msg) {
                switch (msg.what) {
                    case 1:
                        setLvExpert();
                        break;
                }
            }
        };

        new Thread(new Runnable() {
            @Override
            public void run() {
                Looper.prepare();
                String param = "";
                Log.i("check", "here");
                HttpThread httpThread = new HttpThread(LogActivity.url + "/getExpertMsg", param);
                httpThread.run();
                lvData = httpThread.getResult();
                Log.i("check", "lvData = " + lvData);
                Message message = new Message();
                message.what = 1;
                handler.sendMessage(message);
                Looper.loop();
            }
        }).start();

        setLvExpert();
        Log.i("check", "NormalAppoint initView end");
    }

    private void setLvExpert() {
        Log.i("check", "setLvExpert initView start");
        List<Map<String, Object>> data = getData();
        if(data == null) return;
        SimpleAdapter adapter = new SimpleAdapter(this, data, R.layout.expert_detail,
                new String[]{"detail", "remain"}, new int[]{R.id.tvExpertDetail, R.id.tvRemain});
        lvExpert.setAdapter(adapter);
    }

    private List<Map<String, Object>> getData() {
        if(lvData == null || lvData.equals("")) return null;
        Log.i("check", "getData start");
        Map<String, Object> item = new HashMap<>();
        JSONArray jsonArray;
        List<Map<String, Object>> items = null;
        Log.i("check", "1");
        try {
            Log.i("check", "2");
            Log.i("check", "data = " + lvData);
            jsonArray = new JSONArray(lvData);
            Log.i("check", "3");
            items = new ArrayList<>();
            for(int i = 0; i < jsonArray.length(); ++i) {
                JSONObject jsonObject = new JSONObject(jsonArray.getString(i));
                item.put("expertID", jsonObject.getString("expertID"));
                item.put("detail", jsonObject.getString("detail"));
                item.put("remain", jsonObject.getString("remain"));
                items.add(item);
            }
            Log.i("check", "3");
        } catch (JSONException e) {
            e.printStackTrace();
        }
        Log.i("check", "getData end");
        return items;
    }

    @Override
    public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
        Map<String, Object> map = (Map<String, Object>) lvExpert.getItemAtPosition(i);
        Log.i("qwer", "onItemClick");
        Intent intent = new Intent(this, MakeAppointment.class);
        Log.i("check", "onItemClick " + map.get("expertID"));
        intent.putExtra("expertID", String.valueOf(map.get("expertID")));
        intent.putExtra("appointMethod", "expert");
        intent.putExtra("expertDetail", "no...");
        startActivity(intent);
    }
}
